package com.sr.examination.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sr.examination.R;
import com.sr.examination.main.Item;


/**
 * Created by Administrator on 2017/6/1 0001.
 */

public class NavigationBottomView extends LinearLayout implements View.OnClickListener {


    private Item currentItem;
    private OnItemClickListener listener;

    public NavigationBottomView(Context context) {
        super(context);
        init(context);
    }

    public NavigationBottomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View root = inflater.inflate(R.layout.main_navigation_bottom, this, false);
        View homeRoot = root.findViewById(R.id.homeRootLayout);
        Item.HOME.setRoot(homeRoot);
        Item.HOME.setTextView((TextView) homeRoot.findViewById(R.id.homeTView));
        Item.HOME.setImageView((ImageView) homeRoot.findViewById(R.id.homeIView));
        homeRoot.setTag(Item.HOME);
        homeRoot.setOnClickListener(this);
        addView(root);
    }

    public int getPages() {
        return Item.values().length;
    }

    public Item getCurrentItem() {
        return currentItem;
    }

    public void setCurrentItem(Item item) {
        onItemSelected(item);
    }

    public void setCurrentItem(int index) {
        for (int i = 0; i < Item.values().length; i++) {
            if (Item.values()[i].getIndex() == index) {
                onItemSelected(Item.values()[i]);
            }
        }
    }

    public OnItemClickListener getListener() {
        return listener;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(Item item);

        void onItemClickRepeat(Item item);
    }

    @Override
    public void onClick(View v) {
        Item item = (Item) v.getTag();
        onItemSelected(item);
    }

    private void onItemSelected(Item item) {
        if (item == null) {
            return;
        }
        if (item.isRepeatableClick() && (currentItem != null && (currentItem == item || currentItem.getIndex() == item.getIndex() || item.isSelected()))) {
            if (listener != null) {
                listener.onItemClickRepeat(item);
            }
            return;
        }
        setNavigationBottomStatus(item);
        if (listener != null) {
            listener.onItemClick(item);
        }
        if (item.needSelect) {
            currentItem = item;
        }
    }

    /**
     * 设置底部菜单状态
     */
    private void setNavigationBottomStatus(Item item) {
        if (item == null) {
            return;
        }
        if (!item.needSelect) {
            return;
        }
        if (item.getIndex() < 0 || item.getIndex() > Item.values().length) {
            return;
        }
        for (int i = 0; i < Item.values().length; i++) {
            Item.values()[i].setSelected(false);
        }
        item.setSelected(true);
    }
}
